package com.blog.servlet;

import java.io.IOException;
import java.util.Vector;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.blog.dao.ArticleDao;
import com.blog.dao.Article_tagDao;
import com.blog.dao.TagDao;
import com.blog.dao.UserDao;
import com.blog.dto.Article;
import com.blog.dto.Article_tag;
import com.blog.util.DateTime;
public class ArticleServlet extends HttpServlet {

	protected void addArticle(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//设置请求和响应的编码统一为UTF-8
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        int user_id = Integer.parseInt(req.getParameter("user_id"));
        //拿到编辑器的内容
        Article article = new Article();
        article.setAuthor(req.getParameter("author"));
        article.setTitle(req.getParameter("title"));
        article.setDescription(req.getParameter("description"));
        article.setKeyword(req.getParameter("keyword"));
        String cover = req.getParameter("cover");//获取图片路径
        article.setImage(cover);
        article.setCategory(req.getParameter("category"));
//        System.out.println(req.getParameter("category"));
        //获取文章编写内容
        String content = req.getParameter("editorValue");
        article.setContent(content);
        //获取标签tag
        String[] tag = req.getParameterValues("tag");
        //System.out.println(tag);
        if(tag!=null) {
	        //分析标签,并添加到tag表
	        TagDao tagDao = new TagDao();
	        for(String t : tag) {
	        	if(!tagDao.findTagByUser_idAndTag(user_id, t)) {
	        		tagDao.addTagByUserIdAndTagName(user_id, t);
	        	}
	        }
	        //处理article表的标签
	        String strTag = "";
	        if(tag.length!=0)
		        for(int i=0;i<tag.length;i++)
		        	strTag+=","+tag[i];
	        strTag=strTag.substring(1, strTag.length());
	//        System.out.println(strTag);
	
	        //存入标签tag
	        article.setTag(strTag );
        }
        //其他内容
        article.setIscmd(0);
        article.setPublish_time(DateTime.getDateTime());
        article.setReader(0);
        article.setReview(0);
//        System.out.println("图片路径："+cover);
        //如果不为空
        if(content != null){
            //将内容设置进属性
//            req.setAttribute("content",content);
        	//把文章插入数据库
        	ArticleDao articleDao = new ArticleDao();
        	articleDao.addArticle(article);
            Article_tagDao article_tagDao = new Article_tagDao();
            if(tag!=null)
            for(String t : tag) {
            	////插入文章与标签的联系
            	article_tagDao.addLastArticle_tag(user_id, t);
            }
            
            //转发到content.jsp
            req.getRequestDispatcher("admin/article.jsp").forward(req, resp);
        }else{
            resp.getWriter().append("内容为空!");
        }
	}
	
	protected void editArticle(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//设置请求和响应的编码统一为UTF-8
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        int user_id = Integer.parseInt(req.getParameter("user_id"));
        int article_id = Integer.parseInt(req.getParameter("article_id"));
        //拿到编辑器的内容
        Article article = new Article();
        article.setArticle_id(article_id);
        article.setAuthor(req.getParameter("author"));
        article.setTitle(req.getParameter("title"));
        article.setDescription(req.getParameter("description"));
        article.setKeyword(req.getParameter("keyword"));
        String cover = req.getParameter("cover");//获取图片路径
        article.setImage(cover);
        article.setCategory(req.getParameter("category"));
//        System.out.println(req.getParameter("category"));
        //获取文章编写内容
        String content = req.getParameter("editorValue");
        article.setContent(content);
        //获取标签tag
        String[] tag = req.getParameterValues("tag");
        
        //分析标签,并添加到tag表
        TagDao tagDao = new TagDao();
        for(String t : tag) {
        	if(!tagDao.findTagByUser_idAndTag(user_id, t)) {
        		tagDao.addTagByUserIdAndTagName(user_id, t);
        	}
        }
        
        //处理article表的标签
        String strTag = "";
        if(tag.length!=0)
	        for(int i=0;i<tag.length;i++)
	        	strTag+=","+tag[i];
        strTag=strTag.substring(1, strTag.length());
        System.out.println(strTag);

        //存入标签tag
        article.setTag(strTag );
        //其他内容
        article.setIscmd(0);
        article.setPublish_time(DateTime.getDateTime());
        article.setReader(0);
        article.setReview(0);
//        System.out.println("图片路径："+cover);
        //如果不为空
        if(content != null){
            //将内容设置进属性
//            req.setAttribute("content",content);
        	//修改数据库的文章
        	ArticleDao articleDao = new ArticleDao();
        	articleDao.editArticleById(article);
        	
        	//1.删除article_tag的联系，硬删除
    		Article_tagDao article_tagDao = new Article_tagDao();
    		article_tagDao.delHardArticle_tagByArtId(article_id);
    		
        	//2.插入文章与标签的联系
            for(String t : tag) {
            	article_tagDao.addArticle_tag(user_id, article_id ,t);
            }
            
            //转发到content.jsp
            req.getRequestDispatcher("admin/article.jsp").forward(req, resp);
        }else{
            resp.getWriter().append("文章添加失败，内容为空!");
        }
	}
	
	protected void delArticle(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//		int user_id = Integer.parseInt(req.getParameter("user_id"));
		int article_id = Integer.parseInt(req.getParameter("article_id"));
		ArticleDao articleDao =  new ArticleDao();
		articleDao.delArticleById(article_id);
		findAllArticle(req, resp);
		return;
		
	}
	
	protected void findAllArticle(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		Vector<Article> vArticle=new Vector<Article>();
		ArticleDao articleDao=new ArticleDao();
		vArticle = articleDao.findAllArticle();
		HttpSession session = req.getSession();
		session.setAttribute("allArticle", vArticle);
		return ;
	}
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String act = null;
		act=req.getParameter("act");
		
		if(act==null || act.equals("")) act = "index";
		System.out.println("ArticleServlet act = "+act);
		
		switch (act) {
		case "add":
			addArticle(req, resp);
			break;
		case "del":
			delArticle(req, resp);
			break;
		case "edit":
			editArticle(req, resp);
			break;
		case "findAllArticle":
			findAllArticle(req, resp);
			resp.sendRedirect(req.getContextPath()+"/admin/article-list.jsp");
			break;
		case "findUserByName":
			
			break;

		default:
			findAllArticle(req, resp);
//			页面重定向
			resp.sendRedirect(req.getContextPath()+"/admin/article-list.jsp");
//			页面转发
//			req.getRequestDispatcher("/index.jsp").forward(req, resp);
			break;
				
		}
	}

}
